//
//  AppMetricKit.swift
//  Runner
//
//  Created by Longxiang Guo (QXQ4633) on 2021/9/6.
//
import MetricKit
import Foundation

/*
     DYLD_PRINT_OPTS
     DYLD_PRINT_ENV
     DYLD_PRINT_LIBRARIES
     DYLD_BIND_AT_LAUNCH //
     DYLD_DISABLE_DOFS
     DYLD_PRINT_APIS  //
     DYLD_PRINT_BINDINGS //
     DYLD_PRINT_INITIALIZERS //
     DYLD_PRINT_REBASINGS  //
     DYLD_PRINT_SEGMENTS
     DYLD_PRINT_STATISTICS //
     DYLD_PRINT_DOFS
     DYLD_PRINT_RPATHS
 */

@available(iOS 13.0, *)
class AppMetricKit: NSObject, MXMetricManagerSubscriber {

    static let sharedInstance = AppMetricKit()
    
    @available(iOS 13.0, *)
    func startMetricKit() {
        MXMetricManager.shared.add(self)
    }
    
    @available(iOS 13.0, *)
    func stopMetricKit() {
        MXMetricManager.shared.remove(self)
    }
    
    func didReceive(_ payloads: [MXMetricPayload]) {
        payloads.forEach { payload in
            Logger.logMessage(message: String(data: payload.jsonRepresentation(), encoding: String.Encoding.utf8) ?? "")
        }
    }
    
    @available(iOS 14.0, *)
    func didReceive(_ payloads: [MXDiagnosticPayload]) {
        payloads.forEach { payload in
            Logger.logMessage(message:String(data: payload.jsonRepresentation(), encoding: String.Encoding.utf8) ?? "")
        }
    }
}
